<template>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto">
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="副标题" prop="subName">
            <el-input v-model="formData.subName" />
        </el-form-item>
        <el-form-item label="状态">
            <el-select v-model="formData.status" placeholder="请选择状态" style="width: 240px">
                <el-option v-for="item in productStatusData" :key="item.code" :label="item.message"
                    :value="item.code" />
            </el-select>
        </el-form-item>

        <el-form-item label="图片">
            <el-space wrap>
                <Tuku v-model="formData.img1"></Tuku>
                <Tuku v-model="formData.img2"></Tuku>
                <Tuku v-model="formData.img3"></Tuku>
                <Tuku v-model="formData.img4"></Tuku>
                <Tuku v-model="formData.img5"></Tuku>
            </el-space>

        </el-form-item>
        <el-form-item label="分类">
            <CategoryTree v-model="formData.categoryId" title="请选择分类" :strictly="false"></CategoryTree>
        </el-form-item>
        <el-form-item label="标签">
            <el-space wrap>
                <el-tag v-for="tag in formData.tags" :key="tag" closable :disable-transitions="false"
                    @close="handleClose(tag)">
                    {{ tag }}
                </el-tag>
                <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue" size="small"
                    @keyup.enter="handleInputConfirm" @blur="handleInputConfirm" />
                <el-button v-else class="button-new-tag" size="small" @click="showInput">
                    + 新增标签
                </el-button>
            </el-space>



        </el-form-item>
        <el-form-item label="排序">
            <el-input-number v-model="formData.seq" :min="1" />
        </el-form-item>
        <el-form-item label="备注">
            <Editor v-model:modelValue="formData.brief"></Editor>
        </el-form-item>
        <el-form-item class="button">
            <el-button type="primary" @click="onCheckAndSubmit(formRef)">新增</el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">

import CategoryTree from '@/components/CategoryTree.vue';
import { ElMessage } from 'element-plus'


const InputRef = ref()
const inputVisible = ref(false)
const inputValue = ref('')
const formRef = ref();
const formData = reactive<any>({
    name: '小米SU7',
    subName: "小米SU7副标题",
    status: 1,
    brief: "小米SU7备注备注",
    img: "",
    img1: "",
    img2: "",
    img3: "",
    img4: "",
    img5: "",
    tags: ["小米", "新能源"],
    categoryId: 0,
    seq: 1
})
const productStatusData = ref([])

onMounted(() => {
    callProductStatusData()
})
const callProductStatusData = () => {
    enumApi.select({ enumClassName: "ProductStatus" }).then((res: any) => {
        productStatusData.value = res
    })
}
const showInput = () => {
    inputVisible.value = true
    nextTick(() => {
        InputRef.value!.input!.focus()
    })
}
const handleClose = (tag: string) => {
    formData.tags.splice(formData.tags.indexOf(tag), 1)
}
const handleInputConfirm = () => {
    if (inputValue.value) {
        formData.tags.push(inputValue.value)
    }
    inputVisible.value = false
    inputValue.value = ''
}
const onSubmit = () => {

    let imgArr: any = []

    for (let i = 1; i < 6; i++) {
        if (formData["img" + i] != "") {
            imgArr.push(formData["img" + i])
        }
    }

    if (imgArr.length < 1) {
        ElMessage.error('请上传图片')
        return

    }


    let body: any = {}
    body.name = formData.name
    body.subName = formData.subName
    body.status = formData.status
    body.brief = formData.brief
    body.img = imgArr
    body.seq = formData.seq
    body.categoryId = formData.categoryId
    body.tags = formData.tags
    body.lastUpdateBy = formData.lastUpdateBy
    productApi.insert(body).then((res: any) => {
        ElMessage.success('新增成功')

    })
}
const rules = reactive<any>({
    name: [
        { required: true, message: '请输入商品名称', trigger: 'blur' },
    ],
    subName: [
        { required: true, message: '请输入商品副标题', trigger: 'blur' },
    ]

})
const onCheckAndSubmit = async (formEl: any) => {

    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            onSubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}
const resetForm = (formEl: any) => {

    formEl.resetFields()
}
</script>